package com.df.agent;

import com.df.agent.plugin.IPlugin;
import com.df.agent.plugin.InterceptPoint;
import com.df.agent.plugin.PluginFactory;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.utility.JavaModule;

import java.lang.instrument.Instrumentation;
import java.util.List;

public class QiYeAgent {
    //JVM 首先尝试在代理类上调用以下方法
    public static void premain(String agentArgs, Instrumentation inst) {
        System.out.println("handler和common脚本 方法链路追踪开启");
        System.out.println("====================================================== == ==\r\n");
//        AgentBuilder agentBuilder = new AgentBuilder.Default().with(AgentBuilder.LambdaInstrumentationStrategy.ENABLED);
        AgentBuilder agentBuilder = new AgentBuilder.Default();
        List<IPlugin> pluginGroup = PluginFactory.pluginGroup;
        for (IPlugin plugin : pluginGroup) {
            InterceptPoint[] interceptPoints = plugin.buildInterceptPoint();
            for (InterceptPoint point : interceptPoints) {
                AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule) -> {
                    builder = builder.visit(Advice.to(plugin.adviceClass()).on(point.buildMethodsMatcher()));
                    return builder;
                };
                agentBuilder = agentBuilder.type(point.buildTypesMatcher()).transform(transformer);
//                agentBuilder = agentBuilder.type(point.buildTypesMatcher()).transform(transformer).asTerminalTransformation();
            }
        }
        //监听
        AgentBuilder.Listener listener = new AgentBuilder.Listener() {
            @Override
            public void onDiscovery(String s, ClassLoader classLoader, JavaModule javaModule, boolean b) {
            }

            @Override
            public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, boolean b, DynamicType dynamicType) {

//                System.out.println("onTransformation：" + typeDescription);
            }

            @Override
            public void onIgnored(TypeDescription typeDescription,
                                  ClassLoader classLoader, JavaModule javaModule, boolean b) {
            }

            @Override
            public void onError(String s, ClassLoader classLoader,
                                JavaModule javaModule, boolean b, Throwable throwable) {
            }

            @Override
            public void onComplete(String s, ClassLoader classLoader,
                                   JavaModule javaModule, boolean b) {
            }
        };
        agentBuilder.with(listener).installOn(inst);
    }


}

